# -*- coding: utf-8 -*-

import pymssql

class MSSql:
    def __init__(self, host, port, user, pwd, db):
        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd
        self.db = db

    def __GetConnect(self):
        if not self.db:
            raise(nameError, "没有配置数据库信息")

        self.conn = pymssql.connect(host = self.host, port = self.port,
                                    user = self.user, password = self.pwd, database = self.db, charset = 'utf8')
        cur = self.conn.cursor()

        if not cur:
            raise(NameError,"连接数据库失败")
        else:
            return cur
    def ExecQuery(self, sql):
        """
        执行查询语句
        :param sql:
        :return:
        """
        cur = self.__GetConnect()
        cur.execute(sql)
        resList = cur.fetchall()

        self.conn.close()
        return resList

    def ExecNonQuery(self, sql):
        """
        执行非查询语句
        :param sql:
        :return:
        """
        cur = self.__GetConnect()
        cur.execute(sql)
        self.conn.commit()
        self.conn.close()


if __name__ == '__main__':
    ms = MSSql(host = "172.16.101.153", port = '1433', user = 'YmtRelease', pwd = '123456', db = 'YmtRelease')
    resList = ms.ExecQuery("select TOP 1 mobile, mobileVerifyCode from "
                           "[YmtRelease].[dbo].[Ymt_SellerVerifyRequest]order by CreateTime desc;")

    for (mobile, mobileVerifyCode) in resList:
        print mobile, mobileVerifyCode